package com.zhuiyun.pm.client.fb.service.serviceImpl;
import cn.hutool.core.util.RandomUtil;
import com.zhuiyun.pm.client.fb.entity.ClientGoodInfo;
import com.zhuiyun.pm.client.fb.entity.Specs;
import com.zhuiyun.pm.client.fb.mapper.PublishMapper;
import com.zhuiyun.pm.client.fb.mapper.SpecsMapper;
import com.zhuiyun.pm.client.fb.service.PublishService;
import com.zhuiyun.pm.config.CommonResult;
import com.zhuiyun.pm.utils.PrimaryKeyUtil;
import com.zhuiyun.pm.utils.UploadFileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PublishSelvice
 * Description 发布service实现
 * Author Administrator
 * @Date 2023/3/13 PublishSelvice
 * Version 1.0
 **/
@Service
public class PublishServiceImpl implements PublishService {

    @Autowired
    PublishMapper publishMapper;
    @Autowired
    UploadFileUtils uploadFile;
    @Autowired
    SpecsMapper specsMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
  /**
   * 商品发布随机数统一
   * @author 温
   * @date 2023/3/14 11:06
   * @parameter [files, publish]
   * @return int
   */
    @Override
    public int publishAdd(MultipartFile[] goodsImg,MultipartFile[] viewImg, ClientGoodInfo publish, List<Specs> specs) {
        publish.setCheckStatus(1);
        publish.setCreateTime(new Date());
        publish.setUpdateTime(new Date());
        publish.setDelFlag(0);
        LocalDate now = LocalDate.now();
        String goodsNumber = primaryKeyUtil.getOrderId();
        publish.setGoodsNumber(goodsNumber);
        int i=0;
        if(goodsImg!=null){
            String[] imgId = new String[goodsImg.length];
            for (int ids=0;ids <goodsImg.length;ids++) {
                String upl = uploadFile.uploadFile(goodsImg[ids]);
                if (!StringUtils.isEmpty(upl)){
                    imgId[ids] = upl;
                }else{
                    return 0;
                }
            }
                publish.setImgId(StringUtils.join(imgId,','));
        }
        if(viewImg!=null){
            String[] s = new String[viewImg.length];
            for (int a=0;a <viewImg.length;a++) {
                String b = uploadFile.uploadFile(viewImg[a]);
                if (!StringUtils.isEmpty(b)){
                    s[a] = b;
                }else{
                    return 0;
                }
            }
            publish.setImgViewId(StringUtils.join(s,','));
        }
             i = publishMapper.insertSelective(publish);
             if(i > 0 && specs!= null){
                i = specsMapper.insertSpecsAndGoodsNumber(specs,goodsNumber);
             }
        return i;
    }

    /**
     *  修改拍卖信息
     * @author 温
     * @date 2023/3/13 15:42
     * @parameter [file, publish]
     * @return int
     */
    @Override
    public int publishUpdate(ClientGoodInfo publish) {
        int i = publishMapper.updateByPrimaryKeySelective(publish);
        return i;
    }

/**
 * 修改发布的信息测试版
 * @author 温
 * @date 2023/3/20 10:11
 * @parameter [goodsImg, clientGoodInfo]
 * @return int
 */
    @Override
    public int publishUpdates(MultipartFile[] goodsImg, ClientGoodInfo clientGoodInfo) {
        //假设goodsImg是一个多图片上传数组
        // 数组中每个元素都是MultipartFile类型 表示上传的一个文件
        clientGoodInfo.setDelFlag(0);
        int i=0;
        if(goodsImg!=null){ // 判断goodImg 是否为空 不为空循环便利数组中每个对象
            String[] imgId = new String[goodsImg.length];
            for (int ids=0;ids <goodsImg.length;ids++) {
                String upl = uploadFile.uploadFile(goodsImg[ids]); // 调用uploadFile 上传该文件 并将上传成功后返回文件id保存imageId 中
                if (!StringUtils.isEmpty(upl)){
                    imgId[ids] = upl;
                }else{
                    return 0;
                }
            }
            clientGoodInfo.setImgId(StringUtils.join(imgId,','));// 将imageId 数组中所有的文件id拼接成一个字符串 用逗号隔开 保存到实体类对象 imageId 属性中
        }
       i= publishMapper.updateByPrimaryKeySelective(clientGoodInfo);
        return i;
    }


    /**
 * 根据id查询竞拍详情和商品详情
 * @author 温
 * @date 2023/3/14 9:00
 * @parameter [id]
 * @return com.zhuiyun.pm.clientFb.entity.Publish
 */
    @Override
    public ClientGoodInfo publishid(int id) {
        ClientGoodInfo publish = publishMapper.selectByPrimaryKey(id);
        return publish;
    }

/**
 * 查询发布物品规格
 * @author 温
 * @date 2023/3/20 10:24
 * @parameter [goodsNumber]
 * @return java.util.List<com.zhuiyun.pm.client.fb.entity.Specs>
 */
    @Override
    public List<Specs> getSpecsByGoodId(String goodsNumber) {
        Specs specs = new Specs();
        specs.setGoodsNumber(goodsNumber);
        return specsMapper.select(specs);
    }
/**
 * 单图片修改测试
 * @author 温
 * @date 2023/3/21 11:45
 * @parameter [file, aaa]
 * @return int
 */
    @Override
    public int publishUpdateAns(MultipartFile file, ClientGoodInfo aaa) {
        int i=0;
        if(file!=null){
            String s = uploadFile.uploadFile(file);
            if(!StringUtils.isEmpty(s)){
            aaa.setImgId(s);
            }
        }
       i= publishMapper.updateByPrimaryKeySelective(aaa);
        return i;
    }

    /**
     * @Author 赵笛
     * @Description TODO 修改
     * @Date 2023/3/30 16:47
     * @Return int
     * @param goodsImg
     * @param viewImg
     * @param publish
     */

    @Override
    public int updatePublish(MultipartFile[] goodsImg, MultipartFile[] viewImg, ClientGoodInfo publish) {
        publish.setDelFlag(0);
        int i=0;
        if(goodsImg!=null){
            String[] imgId = new String[goodsImg.length];
            for (int ids=0;ids <goodsImg.length;ids++) {
                String upl = uploadFile.uploadFile(goodsImg[ids]);
                if (!StringUtils.isEmpty(upl)){
                    imgId[ids] = upl;
                }else{
                    return 0;
                }
            }
            publish.setImgId(StringUtils.join(imgId,','));
        }
        if(viewImg!=null){
            String[] imgId = new String[viewImg.length];
            for (int ids=0;ids <viewImg.length;ids++) {
                String upl = uploadFile.uploadFile(viewImg[ids]);
                if (!StringUtils.isEmpty(upl)){
                    imgId[ids] = upl;
                }else{
                    return 0;
                }
            }
            publish.setImgViewId(StringUtils.join(imgId,','));
        }
        i= publishMapper.updateByPrimaryKeySelective(publish);
        return i;
    }

    /**
     * 首页模糊查方法
     * @author 温
     * @date 2023/4/6 13:42
     * @parameter [goodsName, startPrice]
     * @return java.util.List<com.zhuiyun.pm.client.fb.entity.ClientGoodInfo>
     */
    @Override
    public List<ClientGoodInfo> publishMO(String goodsName,String startPrice) {
        if(StringUtils.isEmpty(goodsName)){
            goodsName="";
        }
        if(StringUtils.isEmpty(startPrice)){
            startPrice="";
        }
        Example example = new Example(ClientGoodInfo.class);
        example.createCriteria().andLike("goodsName","%"+ goodsName +"%")
                .andLike("startPrice","%"+startPrice+"%")
                .andEqualTo("delFlag",0);
        List<ClientGoodInfo> clientGoodInfos = publishMapper.selectByExample(example);
        return clientGoodInfos;
    }

    /**
     * 模糊查查询到商品信息 根据价格进行由高到低排序
     * @author 温
     * @date 2023/4/6 13:43
     * @parameter [goodName, startPrice]
     * @return java.util.List<com.zhuiyun.pm.client.fb.entity.ClientGoodInfo>
     */
    @Override
    public List<ClientGoodInfo> clientPrice(String goodName,String startPrice) {
        List<ClientGoodInfo> clientGoodInfos = publishMapper.clientPrice(goodName,startPrice);
        return clientGoodInfos;
    }
/**
 * 模糊查查询商品信息 根据价格由低到高来进行排序
 * @author 温
 * @date 2023/4/6 13:44
 * @parameter [goodName, startPrice]
 * @return java.util.List<com.zhuiyun.pm.client.fb.entity.ClientGoodInfo>
 */
    @Override
    public List<ClientGoodInfo> clientPrices(String goodName,String startPrice) {
        List<ClientGoodInfo> clientGoodInfos = publishMapper.clientPrices(goodName,startPrice);
        return clientGoodInfos;
    }

}
